{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f04dd603-a2d1-48ce-8c17-9f1dba8de1ee",
   "metadata": {},
   "source": [
    "Chapter 01\n",
    "\n",
    "# 计算向量大小\n",
    "《线性代数》 | 鸢尾花书：数学不难"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccafb456-2453-4c82-8a65-b1963a370cb2",
   "metadata": {},
   "source": [
    "这段代码的核心任务是计算给定向量 $a$ 和 $b$ 的 **欧几里得范数（L2 范数）**，即它们的长度或模长。在数学上，向量 $a$ 和 $b$ 可以表示为：\n",
    "\n",
    "$$\n",
    "a = \\begin{bmatrix} 3 \\\\ 4 \\end{bmatrix}, \\quad b = \\begin{bmatrix} 1 \\\\ 2 \\\\ 2 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "L2 范数定义如下：\n",
    "\n",
    "$$\n",
    "\\|x\\|_2 = \\sqrt{x_1^2 + x_2^2 + \\dots + x_n^2}\n",
    "$$\n",
    "\n",
    "### 1. 计算向量 $a$ 的 L2 范数\n",
    "\n",
    "$$\n",
    "\\|a\\|_2 = \\sqrt{3^2 + 4^2} = \\sqrt{9 + 16} = \\sqrt{25} = 5\n",
    "$$\n",
    "\n",
    "代码中 `np.linalg.norm(a)` 计算了向量 $a$ 的 L2 范数，并将结果存储在 `length_a` 变量中。同时，`np.sqrt(a[0]**2 + a[1]**2)` 是手动计算范数的方式，等价于上述数学表达式。\n",
    "\n",
    "### 2. 计算向量 $b$ 的 L2 范数\n",
    "\n",
    "$$\n",
    "\\|b\\|_2 = \\sqrt{1^2 + 2^2 + 2^2} = \\sqrt{1 + 4 + 4} = \\sqrt{9} = 3\n",
    "$$\n",
    "\n",
    "代码中 `np.linalg.norm(b)` 计算了向量 $b$ 的 L2 范数，并将结果存储在 `length_b` 变量中。而 `np.sqrt(b[0]**2 + b[1]**2 + b[2]**2)` 通过逐项平方后求和再开方的方式，验证计算结果。\n",
    "\n",
    "### 结论\n",
    "\n",
    "代码的主要目的是通过 **NumPy** 提供的 `np.linalg.norm()` 方法计算向量的 L2 范数，并手动验证计算过程。对于向量 $a$，其模长为 5，对于向量 $b$，其模长为 3。这些范数在计算单位向量、向量归一化、向量距离等应用中非常重要。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37d71df9-b2d5-435a-9f17-dfaab949c3fd",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f39691cd-8baa-40b8-8171-419d2b9ee409",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "659f7ac3-e22f-43d9-b0c4-24567953c3a5",
   "metadata": {},
   "source": [
    "## 定义向量 (一维数组)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "02bd2e0e-511c-40c1-9160-5c717f1e8170",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 4])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([3, 4])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "295ee9fa-b37d-48e7-aa28-d0dad5605d89",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 2])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.array([1, 2, 2])\n",
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab54fa49-fa6d-4ef4-9602-03b50fd99252",
   "metadata": {},
   "source": [
    "## 计算向量长度（L2范数）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1a755646-165a-4d26-a36b-728b8fdbb99d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.0"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "length_a = np.linalg.norm(a)\n",
    "length_a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f1234674-2d4b-499b-827f-37359acf4acb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.0"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sqrt(a[0]**2 + a[1]**2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "04f0ad35-f59e-4de3-83a8-9754f643fb50",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.0"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "length_b = np.linalg.norm(b)\n",
    "length_b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0fe9591a-b2eb-4ed5-b48b-73895160ae83",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.0"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sqrt(b[0]**2 + b[1]**2 + b[2]**2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3763aade-a860-43cb-a495-da8f31ea71ad",
   "metadata": {},
   "source": [
    "作者\t**生姜DrGinger**  \n",
    "脚本\t**生姜DrGinger**  \n",
    "视频\t**崔崔CuiCui**  \n",
    "开源资源\t[**GitHub**](https://github.com/Visualize-ML)  \n",
    "平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
    "\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
    "\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466)  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
